import api from '../../../utils/api';

Page({

  data: {
    prefix: null,
    wordList: [],
    nodataImg: '../../../static/image/nodata.jpg'
  },

  onLoad: function (options) {
    const { prefix } = options;
    this.setData({
      prefix
    }, () => {
      this.getWordList();
    });
  },

  // 请求单词列表
  getWordList() {
    const { prefix } = this.data;
    const _this = this;
    wx.showLoading({
      title: '请稍后...'
    });
    api({
      url: '/User/queryAllWord',
      method: 'POST',
      data: {
        prefix
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          wordList: res.data.data
        });
      }
    });
  },

  // 请求收藏单词
  toLikeword(e) {
    const _this = this;
    const { item } = e.currentTarget.dataset;
    wx.showLoading({
      title: '请稍后...'
    });
    api({
      url: '/User/collectWord',
      method: 'POST',
      data: {
        wordId: item.id
      },
      success: function () {
        wx.hideLoading();
        _this.getWordList();
        if(item.showlike !== 0) {
          wx.showToast({
            title: '取消成功',
            icon: 'success',
            duration: 1000,
            mask: false,
          });
        } else {
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 1000,
            mask: false,
          });
        }
      }
    });
  }
})